package com.glodon.paas.account.dao.mongo;

import java.util.List;

import com.glodon.paas.account.api.bean.AccountConstant;
import com.glodon.paas.account.dao.UserDao;
import com.glodon.paas.account.dao.domain.UserEntity;
import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations;

public class UserDaoMongoImpl  extends MongoDBDao<UserEntity, String> implements UserDao{
	
	public UserEntity getByEmail(String email, Integer status){
		Query<UserEntity> q = this.createQuery().filter("email =", email);
		if(status != null){
			q.filter("status =", status);
		}
		q.order("-status").limit(1);
		return q.get();
	}

	@Override
	public void update(UserEntity user) {
		Query<UserEntity> q = this.createQuery().filter("id =", user.getId());
		super.update(q, user);
	}

	//@Override
	public void updatePassword(String userId, String password) {
		Query<UserEntity> q = this.createQuery().filter("id =", userId);
		UpdateOperations<UserEntity> ops = this.createUpdateOperations().set("password", password);

		this.updateFirst(q, ops);
	}

	//@Override
	public boolean checkValidEmail(String email) {
		Query<UserEntity> q = this.createQuery().filter("email =", email).filter("status !=", AccountConstant.USER_STATUS_INACTIVE);
		return q.get() != null;
	}

	@Override
	public UserEntity getByAccountName(
			String userId, 
			String email,
			String mobile, 
			Integer status) {
		if(userId != null){
			return get(userId);
		}
		Query<UserEntity> q = this.createQuery();
		if(email != null){
			q.filter("email =", email);
		}
		if(email != null){
			q.filter("mobile =", mobile);
		}
		if(status!=null){
			q.filter("status = ", status);
		} 
		return q.get();
	}

	
	/* (non-Javadoc)
	 * @see com.glodon.paas.account.dao.UserDao#getByIds(java.lang.String[])
	 */
	@Override
	public List<UserEntity> getByIds(String[] userIds) {
		throw PaasServerExceptionFactory.nosupport("未实现的方法");
	}

    @Override
    public void addProjectRole(String id,String userId, String projectId, String roleId,String orgId) {
        throw PaasServerExceptionFactory.nosupport("未实现的方法");
    }

	@Override
	public int deleteProjectRole(String userId, String projectId, String roleId,String orgId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateProjectRole(String projectId, String oldeRoleId,
			String newRoleId) {
		// TODO Auto-generated method stub
		return 0;
	}
}

